<?php

class SettingsController extends AdminAppController {
	
	public $uses = array("Admin.Settings");
	
	public function beforeFilter() {
		parent::beforeFilter();
		// the app settings that are persisted in the database
		// (and managed in this controller), are set in app/Config/settings.php
		$this->set("configKeys", Configure::read('CMS.settings.config-keys'));
	}
	
	public function index() {
		$settings = $this->Settings->find('list', array (
			'fields' => array('Settings.value')
		));
		$this->set(compact('settings'));
	}
	
	public function edit() {
		
		if( $this->request->is('post') ) {
			if( !empty($this->data) ) {
				foreach($this->data['config'] as $key => $value) {
					$this->Settings->save( array (
						'key' => $key,
						'value' => $value
					));
				}
				$this->setUserInfo( __('Settings saved.'), 'success' );
				$this->redirect(array('action' => 'index'));
			}
		}
		
		$this->set("settings", $this->Settings->find('list', array (
			'fields' => array('Settings.value')
		)));
		
	}
	
}